package com.shujia.mvc1;

import com.shujia.mysql.User;
import com.shujia.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Demo03Dao {//
    public static User selectUser(String username){
        Connection conn = JDBCUtil.getConn();
        String sql="select * from user where username=?";
        User user = new User();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();

            while (rs.next()){
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }
    public static int insert(String username,String password){
        Connection conn = JDBCUtil.getConn();
        String sql="insert into user(username,password) values(?,?)";
        int i=0;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
